﻿namespace SLClient.Extension
{
    using System;
    using System.IO;
    using System.IO.IsolatedStorage;

    public class IsolatedStorageHelper<T>
    {
        private IsolatedStorageSettings _isolatedStorageSettings;
        private IsolatedStorageFile _storeFile;
        private readonly StorageType _type;

        public IsolatedStorageHelper()
        {
            this._type = StorageType.App;
        }

        public IsolatedStorageHelper(StorageType stroagetype)
        {
            this._type = stroagetype;
        }

        public static bool ApplyStorageSpace(double size)
        {
            bool flag;
            try
            {
                using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    long num = Convert.ToInt64((double) (size * 1024.0));
                    if (file.AvailableFreeSpace < num)
                    {
                        if (!file.IncreaseQuotaTo(num))
                        {
                            return false;
                        }
                        return true;
                    }
                    flag = true;
                }
            }
            catch (IsolatedStorageException)
            {
                flag = false;
            }
            return flag;
        }

        public bool CreateFileInStorage(string filename, T content)
        {
            IsolatedStorageFile storeFileType = this.GetStoreFileType();
            try
            {
                if (!this.FileExists(filename))
                {
                    using (IsolatedStorageFileStream stream = storeFileType.OpenFile(filename, FileMode.Create))
                    {
                        using (StreamWriter writer = new StreamWriter(stream))
                        {
                            writer.Write(content);
                            return true;
                        }
                    }
                }
            }
            catch (IsolatedStorageException)
            {
                return false;
            }
            finally
            {
                if (storeFileType != null)
                {
                    storeFileType.Dispose();
                }
            }
            return false;
        }

        public bool CreateSettingsInStorage(string key, T value)
        {
            IsolatedStorageSettings storeSettingType = this.GetStoreSettingType();
            try
            {
                if (!storeSettingType.Contains(key))
                {
                    storeSettingType.Add(key, value);
                    storeSettingType.Save();
                }
                else
                {
                    storeSettingType[key] = value;
                    storeSettingType.Save();
                }
                return true;
            }
            catch (ArgumentNullException)
            {
                return false;
            }
        }

        public bool DeleteDirInStorage(string path)
        {
            using (IsolatedStorageFile file = this.GetStoreFileType())
            {
                bool flag = false;
                try
                {
                    if (!string.IsNullOrEmpty(path))
                    {
                        string[] fileNames = file.GetFileNames(path + "/*");
                        if (fileNames.Length != 0)
                        {
                            foreach (string str in fileNames)
                            {
                                string str2 = Path.Combine(path, str);
                                file.DeleteFile(str2);
                            }
                        }
                        file.DeleteDirectory(path);
                        flag = true;
                    }
                }
                catch (IsolatedStorageException)
                {
                }
                return flag;
            }
        }

        private bool FileExists(string fileName)
        {
            return this._storeFile.FileExists(fileName);
        }

        public static object GetSettingByKey(string key)
        {
            return IsolatedStorageSettings.ApplicationSettings[key];
        }

        private IsolatedStorageFile GetStoreFileType()
        {
            this._storeFile = (this._type == StorageType.App) ? IsolatedStorageFile.GetUserStoreForApplication() : IsolatedStorageFile.GetUserStoreForSite();
            return this._storeFile;
        }

        private IsolatedStorageSettings GetStoreSettingType()
        {
            this._isolatedStorageSettings = (this._type == StorageType.App) ? IsolatedStorageSettings.ApplicationSettings : IsolatedStorageSettings.SiteSettings;
            return this._isolatedStorageSettings;
        }

        public static bool IsSettingPersisted(string key)
        {
            return IsolatedStorageSettings.ApplicationSettings.Contains(key);
        }
    }
}

